< previous page page_118 next page >

Page 118
EXERCISE
Incrementing a Global Variable
1. Run the sample project.
2. Open the VarExamples.frm form by clicking the Variables command button.
3. Click the Global command button a few times to see the value in the label next to it increment by 1 each time you click.
4. Try closing the form and then running it again to see whether the Global variable has retained its value.
analysis.gifGlobal variables will exist as long as the program is running.
Variables declared with Public or Global are allocated on the heap, an area of memory thats allocated when the program starts and is discarded when the program ends.

Local Variables Mask Public Variables
If you name a Local variable the same name as a module-level or Public variable, the Local variable name will take precedence within that procedure. This means that you wont be able to access the Public variable from within the procedure.
EXERCISE
Local Variables Override Public Variables
1. Stop the project from running.
2. Double-click the Override command button to display the following code:
Private Sub cmdGlobalLocal_Click ()
' You would normally NOT name
' this variable this way
Dim gintCount As Integer

gintCount = 1

lblGlobalLocal.Caption = gintCount
End Sub
analysis.gifNotice how the gintCount variable is set to 1. This sets the value in the Local variable, gintCount, not in the Public variable.

 
< previous page page_118 next page >

If you like this book, buy it!